package com.ruoyi.project.rest.goods.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.rest.goods.domain.RestGoods;
import com.ruoyi.project.rest.goods.service.IRestGoodsService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 商品Controller
 * 
 * @author jfj
 * @date 2022-03-28
 */
@Controller
@RequestMapping("/rest/goods")
public class RestGoodsController extends BaseController
{
    private String prefix = "rest/goods";

    @Autowired
    private IRestGoodsService restGoodsService;

    @RequiresPermissions("rest:goods:view")
    @GetMapping()
    public String goods()
    {
        return prefix + "/goods";
    }

    /**
     * 查询商品列表
     */
    @RequiresPermissions("rest:goods:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(RestGoods restGoods)
    {
        startPage();
        List<RestGoods> list = restGoodsService.selectRestGoodsList(restGoods);
        return getDataTable(list);
    }

    /**
     * 导出商品列表
     */
    @RequiresPermissions("rest:goods:export")
    @Log(title = "商品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(RestGoods restGoods)
    {
        List<RestGoods> list = restGoodsService.selectRestGoodsList(restGoods);
        ExcelUtil<RestGoods> util = new ExcelUtil<RestGoods>(RestGoods.class);
        return util.exportExcel(list, "商品数据");
    }

    /**
     * 新增商品
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存商品
     */
    @RequiresPermissions("rest:goods:add")
    @Log(title = "商品", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(RestGoods restGoods)
    {
        return toAjax(restGoodsService.insertRestGoods(restGoods));
    }

    /**
     * 修改商品
     */
    @RequiresPermissions("rest:goods:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        RestGoods restGoods = restGoodsService.selectRestGoodsById(id);
        mmap.put("restGoods", restGoods);
        return prefix + "/edit";
    }

    /**
     * 修改保存商品
     */
    @RequiresPermissions("rest:goods:edit")
    @Log(title = "商品", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(RestGoods restGoods)
    {
        return toAjax(restGoodsService.updateRestGoods(restGoods));
    }

    /**
     * 删除商品
     */
    @RequiresPermissions("rest:goods:remove")
    @Log(title = "商品", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(restGoodsService.deleteRestGoodsByIds(ids));
    }
}
